﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.Reporting.WebForms;
using System.Configuration;

namespace PeisongWeb.admin
{
    public partial class reportCustomerDetail2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["rb"] != null)
                {
                    string rbno = Request.QueryString["rb"].ToString();
                    string orderno = "";
                    string companyName = "";

                    DataTable dt;

                    if (Request.QueryString["order"] != null)
                    {
                        orderno = Request.QueryString["order"].ToString();

                        DataRow dr = DAL.DbHelper.ExecuteDataRow("dbo.P_Order_getByorderNo", orderno);

                        if (IsUserThreeColSheet(dr["companyId"].ToString()))
                        {
                            ReportViewer1.LocalReport.ReportPath = "admin/reportCustomerDetail_RG.rdlc";
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(Request.QueryString["companyName"]))
                        {
                            companyName = Request.QueryString["companyName"];
                        }
                    }

                    dt = getdata(rbno, orderno,companyName);

                    ReportDataSource rds = new ReportDataSource("reportCustomerDetail_DataTable1", dt);
                    ReportViewer1.LocalReport.DataSources.Clear();
                    ReportViewer1.LocalReport.DataSources.Add(rds);
                    ReportViewer1.LocalReport.Refresh();

                    if (dt.Rows.Count == 0)
                    {
                        ReportViewer1.Visible = false;
                        div_nodata.Visible = true;
                    }
                    else
                    {
                        div_nodata.Visible = false;
                    }
                }
                else
                {
                    Library.Script.ClientMsgUrl("错误的参数。", "reportCustomer.aspx");
                }
            }
        }

        protected DataTable getdata(string rbno,string orderno,string companyName)
        {
            return DAL.DbHelper.ExecuteDataTable("[dbo].[p_report_customer_detail]", rbno, orderno,companyName);
        }

        private bool IsUserThreeColSheet(string companyId)
        {
            string stringArray = ConfigurationManager.AppSettings["ThreeColCompanyId"];
            string[] ids = stringArray.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string item in ids)
            {
                if (item == companyId)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
